﻿//  Created by Mathew Purchase.
//  Copyright (c) 2014 Mathew Purchase. All rights reserved.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Inventory : MonoBehaviour 
{

	//---------------------------------------------------------------------------------------------
	// Properties
	public bool is_Settlement;
	
	
	//---------------------------------------------------------------------------------------------
	// Members
	public Character m_playerMain;
	public GameObject m_playerObject;
	public List<Item> m_inventoryItems;
	public ItemService m_itemService;
	public GameObject m_itemContainer;
	public settlementPiece m_settlementMain;
	
	
	
	//---------------------------------------------------------------------------------------------
	// Events and Delegates
	
	
	//---------------------------------------------------------------------------------------------
	// Init
	
	void Start () 
	{
		if (gameObject.GetComponent<Character>() != null)
		{
			m_playerMain = gameObject.GetComponent<Character>();
		}
		else
		{
			m_playerObject = GameObject.Find ("PlayerCharacter");
			m_playerMain = m_playerObject.GetComponent<Character>();
		}
		
		if (is_Settlement == true)
		{
			m_settlementMain = GetComponent<settlementPiece>();
		}
		
		
		m_itemService = GameObject.Find ("ItemService").GetComponent<ItemService>();
		
		

	}
	
	//---------------------------------------------------------------------------------------------
	// Core Functions

	public bool addToInventory (Item item) 
	{
		bool itemadded = false;
		
		switch (is_Settlement)
		{
			case true:
			{
				for (int a = 0; a < m_inventoryItems.Count; a++)
				{
					if ( m_inventoryItems.Count > 1)
					{
						if (m_inventoryItems[a].ID == item.ID)
						{
							m_inventoryItems[a].Quantity ++;
							//Debug.Log(name + " " +	m_inventoryItems[a].Name + " item incremented " + this);
							
							itemadded = true;
							return true;
						}
					}
					
				}
				if (itemadded == false)
				{
					//Debug.Log(name + " " +	item.Name + " item incremented " + this);
					if (m_playerMain != null)
					{
						m_playerMain.carry ++;
					}
					Item newItem = Instantiate (item) as Item;
					newItem.Quantity = 1;
					newItem.transform.parent = m_itemContainer.transform;
					m_inventoryItems.Add(newItem);
					return true;
				}
				return true;
			}
			case false:
			{
				if (m_playerMain.carry < m_playerMain.maxCarry)
				{
					for (int a = 0; a < m_inventoryItems.Count; a++)
					{
						if (m_inventoryItems[a].ID == item.ID)
						{
							m_inventoryItems[a].Quantity ++;
							Debug.Log(name + " " +	m_inventoryItems[a].Name + " item incremented " + this);
							if (m_playerMain != null)
							{
								m_playerMain.carry ++;
							}
							
							itemadded = true;
							return true;
						}
					}
					if (itemadded == false)
					{
						Debug.Log(name + " " +	item.Name + " item incremented " + this);
						if (m_playerMain != null)
						{
							m_playerMain.carry ++;
						}
						Item newItem = Instantiate (item) as Item;
						newItem.Quantity = 1;
						newItem.transform.parent = gameObject.transform;
						m_inventoryItems.Add(newItem);
						return true;
					}
					return false;
				}
				else 
				return false;
				break;
			}
		
		}
	
		return false;
	
	}
	
	//---------------------------------------------------------------------------------------------
	
	public bool removeFromInventory (Item item)
	{
		bool itemremoved = false;
		
		switch (is_Settlement)
		{
			case true:
			{
			for (int a = 0; a < m_inventoryItems.Count; a++)
			{
				if (m_inventoryItems[a].ID == item.ID)
				{
					if (m_inventoryItems[a].Quantity > 0)
					{
						
						//Debug.Log("item removed");
						itemremoved = true;
						return true;
					}
					
					if (m_inventoryItems[a].Quantity <= 0)
					{
						m_inventoryItems.Remove(item);
						DestroyObject (item.gameObject);
						itemremoved = true;
						return true;
					}
					return true;
				}
				
			}
			break;
			}
			
			
			case false:
			{
				for (int a = 0; a < m_inventoryItems.Count; a++)
				{
					if (m_inventoryItems[a].ID == item.ID)
					{
						if (m_inventoryItems[a].Quantity > 0)
						{
							m_inventoryItems[a].Quantity --;
							if (m_playerMain != null)
							{
								m_playerMain.carry --;
							};
							
							Debug.Log("item removed");
							itemremoved = true;
							return true;
						}
						
						if (m_inventoryItems[a].Quantity == 1)
						{
							if (m_playerMain != null)
							{
								m_playerMain.carry --;
							}
							m_inventoryItems.Remove(item);
							itemremoved = true;
							DestroyObject (item.gameObject);
							return true;
						}
						if (m_inventoryItems[a].Quantity == 0)
						{
							return false;
						}
						return true;
					}
				
				}
			break;
			}
		
		}
		
		
		
		if (itemremoved == false)
		{
			//Debug.LogError(name + " No item to remove! " + this);
			return false;
		}
		return false;
	}
	
	//---------------------------------------------------------------------------------------------
	public bool BuyItem(Item item)
	{
		Debug.Log ("Attempting to Buy item");
		int itemID = item.ID;
		Item itemPurchased = m_itemService.itemList[itemID];
		if ( m_playerMain.gold > itemPurchased.Value)
		{
			if (item.Quantity > 0)
			{
				if (m_playerObject.GetComponent<Inventory>().addToInventory(itemPurchased) == true)
				{
					Debug.Log ("Item Bought");
					m_playerMain.gold += - itemPurchased.Value;
					item.Quantity --;
					return true;
				}
				else 
				{
					return false;
				}
			}
			else 
			{
				return false;
			}
			
			
		}
		else
		{
			return false;
		}

		
	}
	//---------------------------------------------------------------------------------------------
	
	public bool SellItem(Item item)
	{
		Debug.Log ("Attempting to Sell item");
		int itemID = item.ID;
		Item itemSold = m_itemService.itemList[itemID];
		if ( m_settlementMain.gold > itemSold.Value)
		{
			if (m_playerObject.GetComponent<Inventory>().removeFromInventory(itemSold) == true)
			{
				Debug.Log ("Item Sold");
				m_playerMain.gold += itemSold.Value;
				item.Quantity ++;
				return true;
			}
			else
			{
				return false;
			}
			
		}
		else
		{
			return false;
		}
	}		
	//---------------------------------------------------------------------------------------------
}
